package com.suanfa.jiaoheng;

public class 链表反转 {
    public static void main(String[] args) {
        Node headNode = 创造链表();
        headNode = 反转链表(headNode);
        打印链表(headNode);
    }

    static class Node{
        Integer data;
        Node nextNode;

        public Node(Integer data) {
            this.data = data;
        }
    }

    static Node 创造链表(){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        node1.nextNode = node2;
        node2.nextNode = node3;
        node3.nextNode = node4;
        node4.nextNode = node5;
        return node1;

    }

    static Node 反转链表(Node head){
        if(head==null){
            return null;
        }
        Node firstNode = head;
        Node secondNode = firstNode.nextNode;
        Node thridNode;

        firstNode.nextNode = null;

        while(secondNode != null){
            thridNode = secondNode.nextNode;
            secondNode.nextNode = firstNode;

            firstNode = secondNode;
            secondNode = thridNode;
        }
        return firstNode;
    }

    static void 打印链表(Node head){
        Node printNode = head;
        while(printNode!=null){
            System.out.print(printNode.data+"-->");
            printNode = printNode.nextNode;
        }
    }
}
